package jiqiao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author understand
 * @description:
 * @create
 */
public class SortColors {
    public static void main(String[] args) {
        int[] nums = {2, 0, 1};
        sortColors(nums);
        System.out.println(Arrays.toString(nums));
    }

    private static void sortColors(int[] nums) {

        int left = 0, right = nums.length - 1, i = 0;
        while (i <= right) {
            if (nums[i] == 0) {
                swap(nums, i,left);
                left++;
                i++;
            } else if (nums[i] == 2) {
                swap(nums,i , right);
                right--;
            } else
                i++;
        }
    }

    private static void swap(int[] nums, int a, int b) {
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }

}
